##################################################################################################
# 
# CMake script for finding OpenVoronoi.
# 
# Input variables:
# 
# - OpenVoronoi_ROOT_DIR (optional): When specified, header files and libraries will be searched for in
#     ${OpenVoronoi_ROOT_DIR}/include
#     ${OpenVoronoi_ROOT_DIR}/libs
#   respectively, and the default CMake search order will be ignored. When unspecified, the default
#   CMake search order is used.
#   This variable can be specified either as a CMake or environment variable. If both are set,
#   preference is given to the CMake variable.
#   Use this variable for finding packages installed in a nonstandard location, or for enforcing
#   that one of multiple package installations is picked up.
# 
# 
# Cache variables (not intended to be used in CMakeLists.txt files)
# 
# - OpenVoronoi_INCLUDE_DIR: Absolute path to package headers.
# - OpenVoronoi_LIBRARY: Absolute path to library.
# 
# 
# Output variables:
# 
# - OpenVoronoi_FOUND: Boolean that indicates if the package was found
# - OpenVoronoi_INCLUDE_DIRS: Paths to the necessary header files
# - OpenVoronoi_LIBRARIES: Package libraries
# 
# 
# Example usage:
# 
#  find_package(OpenVoronoi)
#  if(NOT OpenVoronoi_FOUND)
#    # Error handling
#  endif()
#  ...
#  include_directories(${OpenVoronoi_INCLUDE_DIRS} ...)
#  ...
#  target_link_libraries(my_target ${OpenVoronoi_LIBRARIES})
# 
##################################################################################################

# Get package location hint from environment variable (if any)
if(NOT OpenVoronoi_ROOT_DIR AND DEFINED ENV{OpenVoronoi_ROOT_DIR})
  set(OpenVoronoi_ROOT_DIR "$ENV{OpenVoronoi_ROOT_DIR}" CACHE PATH
    "OpenVoronoi base directory location (optional, used for nonstandard installation paths)")
endif()

# Search path for nonstandard package locations
if(OpenVoronoi_ROOT_DIR)
  set(OpenVoronoi_INCLUDE_PATH PATHS "${OpenVoronoi_ROOT_DIR}/include" NO_DEFAULT_PATH)
  set(OpenVoronoi_LIBRARY_PATH PATHS "${OpenVoronoi_ROOT_DIR}/lib"     NO_DEFAULT_PATH)
endif()

# Find headers and libraries
find_path(OpenVoronoi_INCLUDE_DIR NAMES voronoidiagram.hpp PATH_SUFFIXES "openvoronoi" ${OpenVoronoi_INCLUDE_PATH})
find_library(OpenVoronoi_LIBRARY  NAMES openvoronoi PATH_SUFFIXES "openvoronoi" ${OpenVoronoi_LIBRARY_PATH})

mark_as_advanced(OpenVoronoi_INCLUDE_DIR
                 OpenVoronoi_LIBRARY)

# Output variables generation
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(OpenVoronoi DEFAULT_MSG OpenVoronoi_LIBRARY
                                                      OpenVoronoi_INCLUDE_DIR)

set(OpenVoronoi_FOUND ${OPENVORONOI_FOUND}) # Enforce case-correctness: Set appropriately cased variable...
unset(OPENVORONOI_FOUND) # ...and unset uppercase variable generated by find_package_handle_standard_args

if(OpenVoronoi_FOUND)
  set(OpenVoronoi_INCLUDE_DIRS ${OpenVoronoi_INCLUDE_DIR})
  set(OpenVoronoi_LIBRARIES ${OpenVoronoi_LIBRARY})
endif()
